import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import copy from 'rollup-plugin-copy' // 引入 rollup-plugin-copy 通过复制可以直接把需要的文件复制到对应的目录中，这些复制的文件不需要构建，不需要压缩，只需要复制到对应的目录中即可。
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig({
  root: 'src/', // 指定根目录为 src
  plugins: [
    vue(),
    copy({
      targets: [
        { src: 'manifest.json', dest: 'dist' }, // 复制 manifest.json 到 dist 目录
        { src: 'src/icons/**', dest: 'dist/icons' }, // 复制 src/icons/** 到 dist/icons 目录
      ],
    }),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [
        ElementPlusResolver({
          importStyle: 'css',
          components: ['ElButton', 'ElLink'],
        }),
      ],
    }),
  ],
  build: {
    outDir: path.resolve(__dirname, 'dist'),
    rollupOptions: {
      input: {
        popup: path.resolve(__dirname, 'src/popup/index.html'),
        contentPage: path.resolve(__dirname, 'src/contentPage/index.html'),
        content: path.resolve(__dirname, 'src/content/content.js'),
        background: path.resolve(__dirname, 'src/background/service-worker.js'),
      },
      output: {
        assetFileNames: 'assets/[name]-[hash].[ext]', // 静态资源
        chunkFileNames: 'js/[name]-[hash].js', // 代码分割中产生的 chunk
        entryFileNames: (chunkInfo) => {
          // 入口文件
          const baseName = path.basename(
            chunkInfo.facadeModuleId,
            path.extname(chunkInfo.facadeModuleId)
          )
          const saveArr = ['content', 'service-worker']
          return `[name]/${
            saveArr.includes(baseName) ? baseName : chunkInfo.name
          }.js`
        },
        name: '[name].js',
      },
    },
  },
})
